import os
import platform
import subprocess
import argparse

def kill_processes(process_name):
    """终止指定名称的进程"""
    system = platform.system()
    
    print(f"正在查找并终止所有{process_name}进程...")
    
    if system == "Windows":
        # Windows平台使用taskkill命令
        try:
            subprocess.run(["taskkill", "/F", "/IM", f"{process_name}.exe"], check=False)
            print(f"已终止所有{process_name}进程")
        except Exception as e:
            print(f"终止{process_name}进程时出错: {e}")
    
    elif system == "Linux" or system == "Darwin":  # Linux或macOS
        # Linux/macOS平台使用pkill命令
        try:
            subprocess.run(["pkill", "-9", process_name], check=False)
            print(f"已终止所有{process_name}进程")
        except Exception as e:
            print(f"终止{process_name}进程时出错: {e}")
    
    else:
        print(f"不支持的操作系统: {system}")

def main():
    parser = argparse.ArgumentParser(description='终止指定类型的进程')
    parser.add_argument('-m', '--matlab', action='store_true', help='终止所有MATLAB进程')
    parser.add_argument('-c', '--cmd', action='store_true', help='终止所有CMD进程')
    
    args = parser.parse_args()
    
    if args.matlab:
        kill_processes("MATLAB")
    elif args.cmd:
        kill_processes("cmd")
    else:
        print("请指定要终止的进程类型: -m 终止MATLAB进程, -c 终止CMD进程")
        parser.print_help()

if __name__ == "__main__":
    main() 